﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using vite_ef.Repositories.EntityFrameworkCore;
using vite_entity.Entities;
using vitecore.Module;

namespace vite_services.Dict
{
    [InjectOn(ServiceLifetime.Scoped, Scheme = InjectScheme.None, Own = true)]
    public class DictServices
    {
        private readonly IBaseRepository<SysDept> _baseDeptRepository;
        private readonly IBaseRepository<SysRole> _baseRoleRepository;

        public DictServices(IBaseRepository<SysDept> baseDeptRepository, IBaseRepository<SysRole> baseRoleRepository)
        {
            _baseDeptRepository = baseDeptRepository;
            _baseRoleRepository = baseRoleRepository;
        }

        public async Task GenerateResponse(Response response, List<SysDept> depts, long parentId)
        {
            var innerDepts = depts.Where(x =>  x.ParentId == parentId);
            foreach (var dept in innerDepts)
            {
                var tempResponse = new Response();
                tempResponse.Id = dept.Id;
                tempResponse.Label = dept.Name;
                tempResponse.Value = dept.Code;
                response.Children.Add(tempResponse);
                await GenerateResponse(tempResponse, depts, dept.Id);
            }
        }

        public async Task<List<Response>> GetDictDataByType(string type)
        {
            var responseList = new List<Response>();
            switch (type)
            {
                case "dept":
                    var depts = await _baseDeptRepository.SelectListWhereAsync(x => x.IsDeleted == 0);
                    foreach (var dept in depts.Where(x=>x.ParentId == 0))
                    {
                        var response = new Response()
                        {
                            Id = dept.Id,
                            Label = dept.Name,
                            Value = dept.Code
                        };
                        await GenerateResponse(response, depts, response.Id);
                        responseList.Add(response);
                    }
                    return responseList;
                case "role":
                  var roles = await _baseRoleRepository.SelectListWhereAsync(x => !x.IsDeleted);
                    foreach (var dept in roles)
                    {
                        var response = new Response()
                        {
                            Id = dept.Id,
                            Label = dept.Name,
                            Value = dept.Code
                        };
                        responseList.Add(response);
                    }
                    return responseList;
                case "sex":
                    responseList.Add(
                        new Response()
                        {
                            Id = 0,
                            Label = "男",
                            Value = "0"
                        });
                    responseList.Add(
                            new Response()
                            {
                                Id = 1,
                                Label = "女",
                                Value = "1"
                            });
                    return responseList;
                default:
                    return responseList;
            }
        }
    }

    public class DepartmentGenerator 
    {
        private readonly Dictionary<int, List<Response>> _departments;

        public DepartmentGenerator(Dictionary<int, List<Response>> departments)
        {
            _departments = departments;
        }

        private void AddDepartment(long Id, string lable, string Value)
        { 
            
        }

    }
}
